﻿USE CodeStash
GO
/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CodeSnippet
	DROP CONSTRAINT FK_CodeSnippet_Language
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CodeSnippet
	DROP CONSTRAINT FK_CodeSnippet_Groupings
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CodeSnippet
	DROP CONSTRAINT FK_CodeSnippet_CodeSnippet
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CodeSnippet
	DROP CONSTRAINT FK_CodeSnippet_CodeCategory
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CodeSnippet
	DROP CONSTRAINT DF_CodeSnippet_Views
GO
ALTER TABLE dbo.CodeSnippet
	DROP CONSTRAINT DF_CodeSnippet_Rating
GO
ALTER TABLE dbo.CodeSnippet
	DROP CONSTRAINT DF_CodeSnippet_TotalRatings
GO
CREATE TABLE dbo.Tmp_CodeSnippet
	(
	CodeSnippetId int NOT NULL IDENTITY (1, 1),
	ActualCode nvarchar(MAX) NOT NULL,
	CodeCategoryId int NOT NULL,
	Description nvarchar(200) NOT NULL,
	Title nvarchar(50) NOT NULL,
	GroupId int NULL,
	LanguageId int NOT NULL,
	AspNetMembershipUserId uniqueidentifier NOT NULL,
	Visibility int NOT NULL,
	Views int NOT NULL,
	Rating int NOT NULL,
	TotalRatings int NOT NULL
	)  ON [PRIMARY]
	 TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_CodeSnippet ADD CONSTRAINT
	DF_CodeSnippet_Views DEFAULT 0 FOR Views
GO
ALTER TABLE dbo.Tmp_CodeSnippet ADD CONSTRAINT
	DF_CodeSnippet_Rating DEFAULT 0 FOR Rating
GO
ALTER TABLE dbo.Tmp_CodeSnippet ADD CONSTRAINT
	DF_CodeSnippet_TotalRatings DEFAULT 0 FOR TotalRatings
GO
SET IDENTITY_INSERT dbo.Tmp_CodeSnippet ON
GO
IF EXISTS(SELECT * FROM dbo.CodeSnippet)
	 EXEC('INSERT INTO dbo.Tmp_CodeSnippet (CodeSnippetId, ActualCode, CodeCategoryId, Description, Title, GroupId, LanguageId, AspNetMembershipUserId, Visibility, Views, Rating, TotalRatings)
		SELECT CodeSnippetId, ActualCode, CodeCategoryId, Description, Title, GroupId, LanguageId, AspNetMembershipUserId, Visibility, Views, Rating, TotalRatings FROM dbo.CodeSnippet WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_CodeSnippet OFF
GO
ALTER TABLE dbo.CodeTag
	DROP CONSTRAINT FK_CodeTag_CodeSnippet
GO
DROP TABLE dbo.CodeSnippet
GO
EXECUTE sp_rename N'dbo.Tmp_CodeSnippet', N'CodeSnippet', 'OBJECT' 
GO
ALTER TABLE dbo.CodeSnippet ADD CONSTRAINT
	PK_CodeSnippet PRIMARY KEY CLUSTERED 
	(
	CodeSnippetId
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
ALTER TABLE dbo.CodeSnippet ADD CONSTRAINT
	FK_CodeSnippet_CodeCategory FOREIGN KEY
	(
	CodeCategoryId
	) REFERENCES dbo.CodeCategory
	(
	CodeCategoryId
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.CodeSnippet ADD CONSTRAINT
	FK_CodeSnippet_CodeSnippet FOREIGN KEY
	(
	Visibility
	) REFERENCES dbo.Visibility
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.CodeSnippet ADD CONSTRAINT
	FK_CodeSnippet_Groupings FOREIGN KEY
	(
	GroupId
	) REFERENCES dbo.Groupings
	(
	GroupId
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.CodeSnippet ADD CONSTRAINT
	FK_CodeSnippet_Language FOREIGN KEY
	(
	LanguageId
	) REFERENCES dbo.Language
	(
	LanguageId
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CodeTag ADD CONSTRAINT
	FK_CodeTag_CodeSnippet FOREIGN KEY
	(
	MatchingSnippetId
	) REFERENCES dbo.CodeSnippet
	(
	CodeSnippetId
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT

